/*
 * @description SignUpEditContactsComponent 动态表单填写组件
 * @author ouxh862
 * Example
 *   <sign-up-edit-contacts
 *     (onDelete)="delCustomer()"
 *     (saveForm)="saveForm($event)"
 *     *ngIf="showModal"
 *     [defaultValue]="contactDefault"
 *     [addition]="addition"
 *   ></sign-up-edit-contacts>
 *  Info
 *  Input()
 *    addition: MatchSignupFieldsResp[]  //自定义表单的字段数组，接口直接返回
 *    defaultValue： MatchSignupFieldsResp  //如果是编辑状态则需要把默认值传进来
 *  Output()
 *     onDelete: true 点击删除按钮;  //当表单有默认值才有删除按钮（即 defaultValue 传值了）
 *     saveForm：DynamicFormDefault;  //点击保存会将表单以 DynamicFormDefault 实例返回；
 */


import {
  Component,
  OnInit,
  ViewEncapsulation,
  ViewChild,
  Input,
  AfterContentInit,
  Output,
  EventEmitter
} from '@angular/core';
import {FormBaseComponent} from "@shared/components/form/form-base/form-base.component";
// import {DynamicFormDefault} from "@interface/shared.model";
import {ToolsService} from "@shared/service/tools/tools.service";
import {DynamicFormDefault} from "@interface/index";


@Component({
  selector: 'sign-up-edit-contacts',
  templateUrl: './sign-up-edit-contacts.component.html',
  styleUrls: ['./sign-up-edit-contacts.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class SignUpEditContactsComponent implements OnInit {
  private _addition: any;
  public _defaultValue: any;

  @Input()
  set addition(val) {
    this._addition = val;
  };

  get addition() {
    return this._addition;
  }

  @Input()
  set defaultValue(val) {
    this._defaultValue = val;
  };

  get defaultValue() {
    return this._defaultValue;
  }

  @Output() saveForm: EventEmitter<any> = new EventEmitter();
  @Output() onDelete: EventEmitter<any> = new EventEmitter();

  @ViewChild(FormBaseComponent) formBase;
  @ViewChild('editWrap') editWrap;

  constructor(private tools: ToolsService) {

  }

  onDel() {
    const data = {
      title: '确定删除？',
      message: ['删除了将不可恢复'],
      cancelBtn: true,
    };
    this.tools.present(data).then(res => {
      if (res) {
        this.onDelete.emit(true);
      }
    });
  }

  sign() {
    let custom = new DynamicFormDefault();
    const signForm = this.formBase.signForm.controls;
    for (const item in signForm) {
      if (item === 'commingName' || item === 'idNo' || item === 'linkPhone' || item === 'sex' || item === 'birthday') {

        custom[item] = signForm[item].value;
      } else {
        const temp = {
          fieldText: item,
          fieldValue: signForm[item].value
        };
        custom['matchSignFieldReqs'].push(temp);
      }
    }
    this.saveForm.emit(custom);
  }

  ngOnInit() {

  }


}
